package com.simple.service.fee;

import com.simple.po.FeeGrant;
import com.xunj.core.CoreDao;

public class FeeGrantService {
	
	private CoreDao dao;

	public FeeGrantService(CoreDao dao) {
		this.dao = dao;
	}

	
	/**
	 * 检测发放费用是否大于存留数
	 * @param filePath
	 * @throws Exception
	 */
	public Double checkFeeGrantAmount(FeeGrant feeGrant) throws Exception
	{
		String hql = "select sum(feeGrantAmount) from FeeGrant where " +
		"feeGrantCompanyName='"+ feeGrant.getFeeGrantCompanyName() + "' and " +
		"areaId='"+ feeGrant.getAreaId() + "' and "+
		"areaDeptId='"+ feeGrant.getAreaDeptId() + "' and "+
		"feeGrantType='预留' and state='在用'";
		if(feeGrant.getFeeGrantId()!=null)
			hql+=" and feeGrantId<>'"+feeGrant.getFeeGrantId()+"'";
		//预留
		Double yl = (Double) dao.findAll(hql).get(0);
		yl=yl==null?0d:yl;
		hql = "select sum(feeGrantAmount) from FeeGrant where " +
			"feeGrantCompanyName='"+ feeGrant.getFeeGrantCompanyName() + "' and " +
			"areaId='"+ feeGrant.getAreaId() + "' and "+
			"areaDeptId='"+ feeGrant.getAreaDeptId() + "' and "+
			"feeGrantType='发放' and state='在用'";
		if(feeGrant.getFeeGrantId()!=null)
			hql+=" and feeGrantId<>'"+feeGrant.getFeeGrantId()+"'";
		//发放
		Double ff = (Double) dao.findAll(hql).get(0);
		ff=ff==null?0d:ff;
		//当前单位预留的数据
		hql = "select sum(billAmountTol+certificateAmountTol) from FeeReport where " +
			"remitCompany='"+ feeGrant.getFeeGrantCompanyName() + "' and " +
			"areaId='"+ feeGrant.getAreaId() + "' and "+
			"areaDeptId='"+ feeGrant.getAreaDeptId() + "' and "+
			"remitState='预留' and reportState='已报销'";
		//报销预留
		Double bxyl = (Double) dao.findAll(hql).get(0);
		bxyl=bxyl==null?0d:bxyl;
		//添加或修改，如操作数据为发放(预留-发放-本次发放)，删除，操作如为预留(预留-发放-本次预留)
		return yl+bxyl-ff;
	}


}
